﻿using IBLL;
using IBLL.DTO;
using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class NumBLL:INumBLL
    {
        public NumBLL(IBase<HouseNum> db)
        {
            Db = db;
        }

        public IBase<HouseNum> Db { get; }
        /// <summary>
        /// 房号录入
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public async Task<int> AddNum(HouseNum num)
        {
            return await Db.AddAsync(num);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelNum(int id)
        {
            return await Db.DeleteAsync(id);
        }
        /// <summary>
        /// 根据编号查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HouseNum GetHouseNum(int id)
        {
            return Db.GetValue(id);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public IQueryable<HouseNum> Show()
        {
            return Db.Show();
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public PageDto ShowWhere(int index, int size, string? name, int id)
        {
            var m = Db.Show();
            if(!string.IsNullOrEmpty(name))
            {
                m = m.Where(x => x.Name.Contains(name));
            }
            if(id>0)
            {
                m = m.Where(x => x.Id == id);
            }
            var c = m.Count();
            var d = m.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();
            PageDto dto = new PageDto();
            dto.Count = c;
            dto.List = d;
            return dto;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public async Task<int> UpdNum(HouseNum num)
        {
            return await Db.UpdateAsync(num);
        }
    }
}
